package com.bjpowernode.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpowernode.boss.constants.BoosConst;
import com.bjpowernode.boss.convert.UserPayConfigConvert;
import com.bjpowernode.boss.domain.UserPayConfig;
import com.bjpowernode.boss.enums.ajax.Code;
import com.bjpowernode.boss.mvc.model.dto.PageInfoDto;
import com.bjpowernode.boss.mvc.model.query.UserPayConfigQuery;
import com.bjpowernode.boss.mvc.model.result.AjaxResult;
import com.bjpowernode.boss.mvc.service.UserPayConfigService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户配置 控制
 */
@RestController
@Slf4j
public class UserPayConfigController {

    @Resource
    private UserPayConfigService userPayConfigService;

    @Resource
    private UserPayConfigConvert userPayConfigConvert;

    @GetMapping("/payConfig/pageList")
    public AjaxResult loadPayConfigData(@RequestParam Integer pageNo) {
        log.info("商户配置 分页查询 第 : {} 页" ,pageNo);
        IPage<UserPayConfig> payConfigIPage = userPayConfigService.loadPayConfigData(pageNo , BoosConst.PAGE_SIZE);

        PageInfoDto pageInfoDto = new PageInfoDto(payConfigIPage.getCurrent(), payConfigIPage.getPages());
        List<UserPayConfig> payConfigList = payConfigIPage.getRecords();
        // 封装
        Map<String , Object> data = new HashMap<>();
        data.put("page", pageInfoDto);
        data.put("list", userPayConfigConvert.toConvertupcvList(payConfigList));
        return AjaxResult.ok(data);
    }

    @PostMapping("/payConfig/create")
    public AjaxResult createPayConfigEdit(@RequestBody @Validated UserPayConfigQuery userPayConfigQuery) {
        log.info("商户支付配置创建信息 :{} " , userPayConfigQuery);
        userPayConfigService.createPayConfigEdit(userPayConfigQuery);
        return AjaxResult.ok();
    }

    @GetMapping("/payCofig/remove")
    public AjaxResult removeUserPayInfo(@RequestParam String userNo) {
        log.info("要删除的商户配置信息 : {}" , userNo);
        if (StrUtil.isNotBlank(userNo)) {
            userPayConfigService.removeUserPayInfoByUserNo(userNo);
            return AjaxResult.ok();
        }
        return AjaxResult.fail(Code.REQUEST_PARAM_ERR);
    }
}
